USE Lib
GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
----------------------------
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_first_val') BEGIN
	DROP  FUNCTION  tsql.fn_first_val
	PRINT '<<< DROP FUNCTION tsql.fn_first_val - Completed with SUCCESS >>>'
END
GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/*================================================================================================
Name:			tsql.fn_first_val
Type:			User defined function 
Owner:					
Description:	Returns first value from a coma sepparated list of values 
Parameters:		@list = list of values comma delimited
Dependens on:  	fn_split
Usage:			SELECT [tsql].[fn_first_val] ('1,2,3,4,5')
--------------------------------------------------------------------------------------------------
Project:		http://sqllib.codeplex.com/
Help:			http://sqllib.codeplex.com/wikipage?title=fn_first_val&referringTitle=Documentation		
Version:		20120904
SrcCtrlApp:		SVN
SrcCtrlPath:	http://sqllib.codeplex.com/SourceControl/list/changesets
--------------------------------------------------------------------------------------------------
History:		09/04/2012 - Adrian E Dudau - Function created
================================================================================================*/

CREATE FUNCTION [tsql].[fn_first_val] (@list NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS

BEGIN
	RETURN (SELECT TOP 1 item_value FROM [tsql].[fn_split](@list,',') ORDER BY id ASC) 
END

GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
-- Verify completion
----------------------------
IF EXISTS(SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_first_val') BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_first_val - Completed with SUCCESS >>>'
END

ELSE BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_first_val - Completed with ERROR >>>'
END

GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------


